<?php

namespace App\Http\Requests\Admin;

use App\Rules\Mobile;
use Illuminate\Foundation\Http\FormRequest;

class UserRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $id = request()->input('id');
        $rules = [
            'name' => 'required|min:2|max:15|unique:users,name,' . $id,
            'mobile' => [
                'required',
                new Mobile(),
                'unique:users,mobile,' . $id,
            ],
            'email' => 'required|email|max:80|unique:users,email,' . $id,
            'password' => 'min:6|max:16',
        ];
        if (in_array($this->method(), ['PUT', 'PATCH'])) {
            $rules['password'] = 'nullable|' . $rules['password'];
        }
        return $rules;
    }

    public function messages()
    {
        return [
            'name.required' => '必须填写用户名！',
            'name.unique' => '用户名已被占用！',
            'name.min' => '用户名长度必须为2-15个字符！',
            'name.max' => '用户名长度必须为2-15个字符！',
            'mobile.required' => '必须填写手机号！',
            'mobile.mobile' => '手机号格式不正确！',
            'mobile.unique' => '手机号已被占用！',
            'email.required' => '必须填写邮箱！',
            'email.email' => '邮箱格式不正确！',
            'email.unique' => '邮箱已被占用！',
            'email.max' => '邮箱地址长度不能超过80！',
            'password.required' => '必须填写密码！',
            'password.min' => '密码长度必须在6到16个字符！',
            'password.max' => '密码长度必须在6到16个字符！',
        ];
    }

}
